<?php
namespace Wechat\Controller;

class ArticleController extends BaseController{
    public function index()
    {
        echo 11;
    }
    /*软文搜索页*/
    public function ser()
    {
        $ser = i('post.ser');
        $where= array('like','%'.$ser.'%');
        if(IS_POST){
            if(trim(empty($ser))){
                $this->error('查询内容不能为空');
            }
            $count      = M('article')->where(['article_title'=>$where])->count();
            if($count==0){
                $this->error('没有找到你要找的内容,抱歉了。');
            }
            $Page       = new \Think\Page($count,10);
            $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
            $show       = $Page->show();
            $list = M('article')->where(['article_title'=>$where])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
            foreach ($list as $k=>$v) {
                $list[$k]['article_type'] = M('articletype')->where(['id' => $v['article_type']])->field('articletype_name')->find();
                if($list[$k]['article_ischeck']==1){
                    $list[$k]['article_ischeck']='审核通过';
                }else{
                    $list[$k]['article_ischeck']='待审核';
                }
            }
            /*
            echo '<pre/>';
            print_r($list);
            die();
            */
            $this->assign(['page'=>$show,'data'=>$list]);
            $this->display();
        }else{
            $this->display();
        }
    }
    /*软文管理主页*/
    public function manger()
    {
        $count      = M('article')->count();
        $Page       = new \Think\Page($count,10);
        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();
        $list = M('article')->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($list as $k=>$v) {
            $list[$k]['article_type'] = M('articletype')->where(['id' => $v['article_type']])->field('articletype_name')->find();
            if($list[$k]['article_ischeck']==1){
                $list[$k]['article_ischeck']='审核通过';
            }else{
                $list[$k]['article_ischeck']='待审核';
            }
        }
        /*
        echo '<pre/>';
        print_r($list);
        die();
        */
        $this->assign(['page'=>$show,'data'=>$list]);
        $this->display();
    }
    /*软文管理添加*/
    public function add()
    {
        $articletype = M('articletype')->field('articletype_name,id')->select();
        $this->assign('articletype',$articletype);
        $post = i('post.');
        if(IS_POST){
            /*
            echo '<pre/>';
            var_dump($post);
            */
            if(trim(empty($post['content'])) || trim(empty($post['thumb']))){
                $this->error('非法上传，请检查输入');
            }
            $data = [
                'article_title'=>$post['title'],
                'article_key'=>$post['key'],
                'article_des'=>$post['des'],
                'article_maker'=>$_SESSION['name'],
                'article_time'=>$post['time'],
                'article_content'=>$post['content'],
                'article_thumb'=>$post['thumb'],
                'article_ischeck'=>$post['check'],
                'article_read'=>$post['read'],
                'article_type'=>$post['type'],
            ];
            if(M('article')->add($data)){
                $this->success('Congratulation!  Successfully added!!!');
            }else{
                $this->error('未知错误');
            }
        }else {
            $this->display();
        }
    }
    /*软文编辑*/
    public function edit()
    {
        $id = i('get.id');
        $old = M('article')->where(['id'=>$id])->find();
        $post = i('post.');
        $this->assign('old',$old);
        $articletype = M('articletype')->field('articletype_name,id')->select();
        $this->assign('articletype',$articletype);
        if(IS_POST){
            if(trim(empty($post['content'])) || trim(empty($post['thumb']))){
                $this->error('非法上传，请检查输入');
            }
            $data = [
                'article_title'=>$post['title'],
                'article_key'=>$post['key'],
                'article_des'=>$post['des'],
                'article_maker'=>$_SESSION['name'],
                'article_time'=>$post['time'],
                'article_content'=>$post['content'],
                'article_thumb'=>$post['thumb'],
                'article_ischeck'=>$post['check'],
                'article_read'=>$post['read'],
                'article_type'=>$post['type'],
            ];
            if(M('article')->where(['id'=>$id])->save($data)){
                $this->success('Congratulation!  Successfully edit!!!');
            }else{
                $this->error('未知错误');
            }
        }else {
            $this->display();
        }
    }
    /*软文删除*/
    public function del()
    {
        $id =i('get.id');
        if(M('article')->where(['id'=>$id])->delete()){
            $this->success('Congratulation!  Successfully del!!!');
        }else{
            $this->error('未知错误');
        }
    }
    /*批量删除*/
    public function getpost()
    {
        $post = i('post.');
        /*
        echo '<pre/>';
        var_dump($post);
        die();
        */
        if(IS_POST) {

            foreach ($post['ids'] as $k) {
                M('article')->where(['id' => $k])->delete();
            }
            $this->redirect(U('Article/manger'));
        }
    }
}